<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sales_m extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
    
	//增加或保存销售项目数据
	function saveProgram($postdata,$store_arr,$program_id=''){
	   
		if($program_id==''){//是新增项目 
			//$postdata['create_time']		=time();
			$this->db->query($this->db->insert_string('db_sales_program', $postdata));
			$insertid = $this->db->insert_id();
		}else{
			//更新项目信息
			$this->db->update('db_sales_program', $postdata, 'id='.$program_id);
			$insertid =$program_id;
		}
        if($insertid>0){//增加店铺的关联项目
            $this->db->query("DELETE FROM db_sales_shop WHERE program_id='$insertid'");//删除以前的关联
            $create_time=time();
            if(count($store_arr)>0){
        		$sql_str="INSERT INTO `db_sales_shop`(program_id, store_id, store_name, create_time) VALUES";
                foreach($store_arr as $key=>$store_val){
                    $insert_data.="($insertid, $store_val[1],'$store_val[0]', $create_time),";
                }
                $insert_data=trim($insert_data,',');
                $sql_str.=$insert_data;
                $this->db->query($sql_str);
            }
        }
        
		return $parentID;
    }
    
    public function addProgramProduct($sku_arr,$programid){
        $create_time=time();
		$sql_str="INSERT INTO `db_sales_goods`(sku_code,goods_id, program_id, create_time) VALUES";
        foreach($sku_arr as $key=>$goodinfo){
            $insert_data.="('$goodinfo[goods_sku]',{$goodinfo[goods_id]}, $programid, $create_time),";
        }
        $insert_data=trim($insert_data,',');
        $sql_str.=$insert_data;
        $this->db->query($sql_str);
        $insertid = $this->db->insert_id();
        return $insertid;
    }
    
        
    public function testInsert(){   
		$rsdb = $this->db->get_one("SELECT * FROM t_overseapurchasing WHERE status = 0 and createUID = '".$uid."' AND seaware_id = '".$seawarehouseID."' AND oversearproduct_id='".$rs."'");
		if($rsdb){
			$sql = "UPDATE t_overseapurchasing SET";
			$sql.=" skunum=skunum+'".$skunum[$key]."'";
		}else{
			$sql = "INSERT INTO t_overseapurchasing SET";
			$sql.=" skunum='".$skunum[$key]."'";
		}
		$sql.=",seawarehouse='".$seawarehouse."'";
		$sql.=",seaware_id='".$seawarehouseID."'";
		$sql.=",shopname='".$shopname."'";
		$sql.=",goodsno='".$goodsno[$key]."'";
		$sql.=",oversearproduct_id='".$rs."'";
		
		$sql.=",remark='".$remark[$key]."'";
		$sql.=",createUser='".$createUser."'";
		$sql.=",createUID='".$uid."'";
		$sql.=",opertime='".date("Y-m-d H:i:s")."'";
		if($rsdb){
			$sql.= " WHERE id='".$rsdb["id"]."'";
		}
		if(($isst[$key]==0 || $ishw[$key]==0) && $skunum[$key]>0){
			$this->db->query($sql);
		}
	}
}

/* End of file Product_m.php */